package com.realbrand.net.realbrandpay.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.bean.GradevinBean;
import com.realbrand.net.realbrandpay.bean.GradevinDetailsBean;
import com.realbrand.net.realbrandpay.ui.activity.shopcar.CommodityDetailsActivity;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2018/7/4.
 */

public class GradevinAdapter extends BaseExpandableListAdapter {
    List<GradevinBean> data;
    Context mContext;
    LayoutInflater inflater;

    public GradevinAdapter(List<GradevinBean> data, Context mContext) {
        this.data = data;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getGroupCount() {
        return data.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return data.get(groupPosition).getmList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return data.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return data.get(groupPosition).getmList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.gradevin_lv_groupitem, parent, false);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        String[] split = data.get(groupPosition).getGoodsImg().split(",");
        Picasso.with(mContext).load(split[0] + split[1]).into(groupViewHolder.iv_img);
        groupViewHolder.tv_goodsName.setText(data.get(groupPosition).getGoodsName());
        groupViewHolder.tv_gradevinName.setText(data.get(groupPosition).getGradevinName());
        groupViewHolder.tv_gradevinAddress.setText(data.get(groupPosition).getGradevinAddress());
        groupViewHolder.tv_goodsPrice.setText(data.get(groupPosition).getGoodsPrice());
        groupViewHolder.tv_goodsSum.setText(data.get(groupPosition).getGoodsSum());
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView=inflater.inflate(R.layout.shopcar_elv_item_layout,parent,false);
            childViewHolder=new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        }else{
            childViewHolder= (ChildViewHolder) convertView.getTag();
        }
        GradevinChildAdapter childAdapter=new GradevinChildAdapter(data.get(groupPosition).getmList(),inflater);
        childViewHolder.listView.setAdapter(childAdapter);
        setListViewHeightBasedOnChildren(childViewHolder.listView);
        childViewHolder.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GradevinDetailsBean gradevinDetailsBean = data.get(groupPosition).getmList().get(position);
                Intent intent = new Intent(mContext, CommodityDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", gradevinDetailsBean);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupViewHolder {
        ImageView iv_img;
        TextView tv_goodsName;
        TextView tv_gradevinName;
        TextView tv_gradevinAddress;
        TextView tv_goodsPrice;
        TextView tv_goodsSum;

        public GroupViewHolder(View view) {
            iv_img = (ImageView) view.findViewById(R.id.gradevin_lv_groupItem_img);
            tv_goodsName = (TextView) view.findViewById(R.id.gradevin_lv_groupItem_commodityName);
            tv_gradevinName = (TextView) view.findViewById(R.id.gradevin_lv_groupItem_storeName);
            tv_gradevinAddress = (TextView) view.findViewById(R.id.gradevin_lv_groupItem_storeAdress);
            tv_goodsPrice = (TextView) view.findViewById(R.id.gradevin_lv_groupItem_money);
            tv_goodsSum = (TextView) view.findViewById(R.id.gradevin_lv_groupItem_number);
        }
    }

    class ChildViewHolder {
        ListView listView;

        public ChildViewHolder(View view) {
            listView = (ListView) view.findViewById(R.id.shopcarElvItem_listView);
        }
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
// 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
// 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() *
                (listAdapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}
